<template>
<view class="tologin layout-col">
  <view class="flex-grow1"></view>
  <view class="flex-grow1">
    <image class="img" src="/static/image/img-to-login.png"></image>
    <view class="text">需要您的授权才能继续使用哦!</view>
    <view class="btn-box">
      <button class="send-btn" open-type="getUserInfo" @getuserinfo="bindGetUserInfo">授权登录</button>
    </view>
  </view>
  <view class="flex-grow1"></view>
</view>
</template>

<script>
var app = getApp();

export default {
  data() {
    return {
      motto: 'Hello World',
      showModal: false,
      userInfo: {},
      hasUserInfo: false,
      canIUse: uni.canIUse('button.open-type.getUserInfo'),
      code: ""
    };
  },

  components: {},
  props: {},
  onLoad: function (options) {
    var that = this;
    that.setData({
      code: options.code
    });
  },
  methods: {
    bindGetUserInfo: function (e) {
      var that = this;
      uni.getSetting({
        success: function (res) {
          if (res.authSetting['scope.userInfo']) {
            uni.getUserInfo({
              success: function (res_user) {
                var jsonData = {
                  code: that.code
                };
                app.globalData.JsonPost('api/login', jsonData, function (data) {
                  console.log(data); //存储openid

                  uni.setStorageSync("openId", data.msg.openid);
                  app.globalData.JsonPost('api/loginByOpenId', {
                    openid: uni.getStorageSync('openId')
                  }, function (data) {
                    console.log('loginByOpenId');
                    console.log(data);

                    if (data) {
                      //存储用户信息
					  //存储用户信息
					  uni.setStorageSync("user", data.data)
					  // 以下是之前版本处理方式
                      uni.setStorageSync("userId", data.data.user_id);
                      uni.setStorageSync("userName", data.data.user_name);
                      uni.setStorageSync("userNick", data.data.user_nick);
                      uni.setStorageSync("userDlass", data.data.user_class);
                      uni.setStorageSync("userGrade", data.data.user_grade);
                      uni.setStorageSync("userRoleId", data.data.user_role_id);
                      uni.setStorageSync("userRole", data.data.user_role.role_name);
                      uni.switchTab({
                        url: '../index/index'
                      });
                    } else {
                      uni.navigateTo({
                        url: `../login/login`
                      });
                    }
                  });
                });
              },
              fail: function () {
                console.log('获取用户信息失败');
              }
            });
          }
        }
      });
    }
  }
};
</script>
<style>
page {
  height: 100%;
}

.tologin {
  height: 100%;
}

.tologin .img {
  display: block;
  width: 140px;
  height: 114px;
  margin: 0 auto;
}

.tologin .text {
  color: #000;
  font-size: 14px;
  text-align: center;
  margin: 15px 0 35px 0;
}

</style>